/*
*******************************************************************************
** Copyright (C), 2014-2020, TONLY AV R&D CENTER SOFTWARE DEVELOPMENT DEPARTMENT
** FileName: 
** <author>:             
** Date:
** Description:
**
** Others:
** Function List:
**   1.
** History:
**  <Author>    <date>    <content>    <version>
*******************************************************************************
*/
//*****************************************************************************
// File Include
//*****************************************************************************
#include "cdn_adv.h"
#include "app_log.h"
#include "cdn_light.h"
#include "app.h"
#include "cdn_pwm.h"

//*****************************************************************************
// Local Defines
//*****************************************************************************

//*****************************************************************************
// Local Data
//*****************************************************************************

//*****************************************************************************
// Global Data
//*****************************************************************************
uint8_t cdn_adv_handle;

//广播数据
uint8_t user_adv[29]=
{
   0x02,0x01,0x06,//0-2
   0x03,0x03,0x27,0x18,//3-6
   0x15,//7
   0x16,//8
   0x27,0x18,//9-10 //GATT service
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//11-26Device ID
   0,0,//27-28 OOB information
 };

//广播回应包数据
uint8_t  user_scanRsp[31]=
{
  0x1e,//0
  0xff,//1
  0x11,0x02,//2-3
  0,0,0,0,0,0,//4-9
  0,0,//10-11
  0,0,0,0,0,0,0,0,//12-19
  0,0,0,0,0,0,0,0,0,0,0 //20-30
};

extern const uint8_t *__mesh_dcd_ptr;

//*****************************************************************************
// API Functions
//*****************************************************************************
//创建一个广播句柄
void mesh_adv_create_init(void)
{
  sl_status_t sc = sl_bt_advertiser_create_set(&cdn_adv_handle);
  if(sc == SL_STATUS_OK)
  {
      TRACE("zsm: mesh_adv_create_init Success !\r\n");
  }
  else
  {
      TRACE("zsm: mesh_adv_create_init Fail = 0x%lx !\r\n",sc);
  }
}

//初始化广播数据
void mesh_user_adv_init(void)
{
  sl_status_t get_uuid_sc = sl_btmesh_node_get_uuid((uuid_128 *)&user_adv[11]);
  if(get_uuid_sc == SL_STATUS_OK)
  {
    TRACE("zsm: sl_btmesh_node_get_uuid Success ");
		for(uint8_t i = 11;i < 27;i++)
		{
			TRACE("%02x",user_adv[i]);
		}
		TRACE("\n");
  }
  else
  {
      TRACE("zsm: sl_btmesh_node_get_uuid Fail = 0x%lx !\r\n",get_uuid_sc);
  }
  sl_status_t advertiser_set_data_sc = sl_bt_advertiser_set_data(cdn_adv_handle,0,sizeof(user_adv),user_adv);
  if(advertiser_set_data_sc == SL_STATUS_OK)
  {
      TRACE("zsm: mesh_user_adv_init Success !\r\n");
  }
  else
  {
      TRACE("zsm: mesh_user_adv_init Fail = 0x%lx !\r\n",advertiser_set_data_sc);
  }
}

//初始化广播回应包数据
void mesh_scan_rsp_init(void)
{
  uint8_t mac_addr_type;
  uint16_t device_type = 0;
  sl_status_t get_identity_address_sc = sl_bt_system_get_identity_address((bd_addr *)&user_scanRsp[4], &mac_addr_type);
  if(get_identity_address_sc == SL_STATUS_OK)
  {
      TRACE("zsm: sl_bt_system_get_MAC_address = %x %x %x %x %x %x \r\n",user_scanRsp[4],user_scanRsp[5],user_scanRsp[6],user_scanRsp[7],user_scanRsp[8],user_scanRsp[9]);
  }
  else
  {
      TRACE("zsm: sl_bt_system_get_MAC_address Fail = 0x%lx \r\n",get_identity_address_sc);
  }
  uint16_t volatile adr_primary = cdn_get_element_address();
  user_scanRsp[10] = adr_primary & 0x00ff;
  user_scanRsp[11] = (adr_primary & 0xff00) >> 8;
  TRACE("zsm: mesh_scan_rsp_init adr_primary = 0x%x\r\n",adr_primary);

  user_scanRsp[20] = 0xA0;

  //色温灯
  CDN_Adv_Type(&user_scanRsp[21]);
  device_type |= user_scanRsp[21];
  device_type = (device_type << 8) | user_scanRsp[22];

  TRACE("lj: mesh_scan_rsp_init Device = 0x%04X\r\n", device_type);

	//强制写成产测模式，可以使产测工具扫描连接
	//user_scanRsp[23] = 0x01;
	if(GetCurrentLightWorkMode() == CDN_FACTORY_MODE)
	  {
	    user_scanRsp[23] = 0x01;
	    TRACE("FactoryMode!!Can be connected by factory tools\n");
	  }
	else
	  {
	    user_scanRsp[23] = 0x00;
	    TRACE("WorkMode!!Can't be connected by factory tools\n");
	  }
	user_scanRsp[24] = *(__mesh_dcd_ptr + 4);
	user_scanRsp[25] = *(__mesh_dcd_ptr + 5);	
	TRACE("zx: Current_Light_Software_Version = 0x%02x%02x\n",user_scanRsp[25],user_scanRsp[24]);
	
  sl_status_t advertiser_set_sc = sl_bt_advertiser_set_data(cdn_adv_handle,1,sizeof(user_scanRsp),user_scanRsp);
  if(advertiser_set_sc == SL_STATUS_OK)
  {
      TRACE("zsm: mesh_scan_rsp_init Success !\r\n");
  }
  else
  {
      TRACE("zsm: mesh_scan_rsp_init Fail = 0x%lx !\r\n",advertiser_set_sc);
  }
}

//启动广播
void cdn_adv_start(void)
{
	sl_bt_advertiser_set_timing(cdn_adv_handle,32,40,0,0);
	sl_bt_advertiser_start(cdn_adv_handle,sl_bt_advertiser_user_data,sl_bt_advertiser_connectable_scannable);
}

//结束广播
void cdn_adv_stop(void)
{
	sl_bt_advertiser_stop(cdn_adv_handle);
}
//更新广播数据，入网后更新，把1827服务修改为1828
void user_adv_update(void)
{
  user_adv[5] = 0x28;
  user_adv[6] = 0x18;
  user_adv[9] = 0x28;
  user_adv[10] = 0x18;
  sl_status_t adv_update_data_sc = sl_bt_advertiser_set_data(cdn_adv_handle,0,sizeof(user_adv),user_adv);
  if(adv_update_data_sc == SL_STATUS_OK)
  {
      TRACE("zsm: user_adv_update Success !\r\n");
  }
  else
  {
      TRACE("zsm: user_adv_update Fail = 0x%lx !\r\n",adv_update_data_sc);
  }
}

//更新回应包数据，更新单播地址
void user_scanRsp_update(void)
{
  uint16_t volatile adr_primary = cdn_get_element_address();
  user_scanRsp[10] = adr_primary & 0x00ff;
  user_scanRsp[11] = (adr_primary & 0xff00) >> 8;
  TRACE("zsm: user_scanRsp_update adr_primary = 0x%x\r\n",adr_primary);

  sl_status_t user_scanRsp_set_sc = sl_bt_advertiser_set_data(cdn_adv_handle,1,sizeof(user_scanRsp),user_scanRsp);
  if(user_scanRsp_set_sc == SL_STATUS_OK)
  {
      TRACE("zsm: user_scanRsp_update Success !\r\n");
  }
  else
  {
      TRACE("zsm: user_scanRsp_update Fail = 0x%lx !\r\n",user_scanRsp_set_sc);
  }
}
//接收手机的广播信息并处理，回应手机的寻找请求
void user_adv_filter_proc(struct sl_bt_msg *evt)
{
  uint8_t mac_addr_type;
  uint8_t CDN_DeviceMac[6];

  uint8_t EscBurnInTest[]={0x11,0x07,0xA0,0x11,0x02,0xFF,0xFF,0x04};
  uint8_t EnterBurnInTest[]={0x11,0x07,0xA0,0x11,0x02,0xFF,0xFF,0x03};
	uint8_t UserAdvDisconectBT[]={0x11,0x07,0xA0,0x11,0x02,0xFF,0xFF,0x02};
  uint8_t UserAdvFilterOn[]={0x11,0x07,0xA0,0x11,0x02,0xFF,0xFF,0x01};
  uint8_t UserAdvFilterOff[]={0x11,0x07,0xA0,0x11,0x02,0xFF,0xFF,0x00};

  sl_bt_system_get_identity_address((bd_addr *)CDN_DeviceMac, &mac_addr_type);
  uint8_t *P_AdvData = evt->data.evt_scanner_scan_report.data.data; //获取广播数据

  if(!memcmp(CDN_DeviceMac,&P_AdvData[9],sizeof(CDN_DeviceMac)))
  {
      if(GetConfigBlinkFlag() == 0)
        {
          SetLedState(CDN_LED_CONFIG_BLINK,0,0);    //闪烁
          SetConfigBlinkFlag(1);
          TRACE("lj: APP ADV CDN_LED_CONFIG_BLINK !");
        }
  }
  else if(!memcmp(&P_AdvData[3],UserAdvFilterOn,sizeof(UserAdvFilterOn))) //广播开灯
  {
    SetLedState(CDN_LED_ON,100,0);
    //TRACE("zsm: APP ADV all device On !");
  }
  else if(!memcmp(&P_AdvData[3],UserAdvFilterOff,sizeof(UserAdvFilterOff))) //广播关灯
  {
    SetLedState(CDN_LED_OFF,0,0);
    //TRACE("zsm: APP ADV all device Off !");
  }
	else if(!memcmp(&P_AdvData[3],UserAdvDisconectBT,sizeof(UserAdvDisconectBT))) //广播通知断开蓝牙
  {
		sl_status_t sc;
		sc = sl_bt_connection_close(GetConnectHandle());
		if(sc == SL_STATUS_OK)
		  {
			TRACE("user_adv_filter_proc sl_bt_connection_close OK\n");
		  }
		else
		  {
			TRACE("user_adv_filter_proc sl_bt_connection_close Fail\n");
		  }
  }
  else if(!memcmp(&P_AdvData[3],EnterBurnInTest,sizeof(EnterBurnInTest))) //进入老化测试
  {
#if(CDN_MAX_POWER_12W_24W)
      SetLightCurrentValue(CDN_24W_Max_Cur);//电流值设为最大
#else
      SetLightCurrentValue(CDN_12W_Max_Cur);//电流值设为最大
#endif
    //TRACE("lj: APP ADV Enter Burn-In Test !\n");
  }
  else if(!memcmp(&P_AdvData[3],EscBurnInTest,sizeof(EscBurnInTest))) //退出老化测试
  {
#if(CDN_MAX_POWER_12W_24W)
      SetLightCurrentValue(CDN_24W_Min_Cur);//电流值默认设为最小
#else
      SetLightCurrentValue(CDN_12W_Min_Cur);//电流值默认设为最小
#endif
    //TRACE("lj: APP ADV Esc Burn-In Test !\n");
  }
}










